package cn.gzsxt.music.mapper;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import cn.gzsxt.music.query.CDQueryObject;
import cn.gzsxt.music.util.StringLength;
@Mapper
public class CDProvider {
	/**
	 * 查询所有专辑或者根据条件模糊查询(带有分页)
	 * @param cqo 模糊查询的条件及分页信息
	 * @return
	 */
	public String selectCDSql(CDQueryObject cqo) {
		StringBuilder cdSql = new StringBuilder();
		cdSql.append("select cd.cd_id,cd.cd_cover,cd.cd_name,singer.singer_name,singer.singer_id,cd.cd_count,cd.cd_publish,cd.cd_like,cd.cd_info from mc_cd cd,mc_singer singer where cd.singer_id = singer.singer_id ");
		
		if(StringUtils.isNotBlank(cqo.getCd_name())) {
			cdSql.append("and cd.cd_name like '%" + cqo.getCd_name() + "%' ");
		}
		
		if(StringUtils.isNotBlank(cqo.getSinger_name())) {
			cdSql.append("and cd.singer_id in (" + cqo.getSinger_id() + ") ");
		}
		
		if(StringUtils.isNotBlank(cqo.getCd_begin())) {
			cdSql.append("and cd_publish >= #{cd_begin}");
		}
		
		if(StringUtils.isNotBlank(cqo.getCd_end())){
			cdSql.append("and cd_publish <= #{cd_end} ");
		}
		cdSql.append("limit #{beginIndex},#{rows}");
		return cdSql.toString();
	}
	
	/**
	 * 查询所有专辑或者根据条件模糊查询的专辑数量
	 * @param cqo 模糊查询的条件及分页信息
	 * @return
	 */
	public String selectCDCountSql(CDQueryObject cqo) {
		StringBuilder cdSql = new StringBuilder();
		cdSql.append("select count(*) from mc_cd cd,mc_singer singer where cd.singer_id = singer.singer_id");
		
		if(StringUtils.isNotBlank(cqo.getCd_name())) {
			cdSql.append(" and cd.cd_name like '%" + cqo.getCd_name() + "%'");
		}
		
		if(StringUtils.isNotBlank(cqo.getSinger_name())) {
			cdSql.append(" and cd.singer_id in (" + cqo.getSinger_id() + ")");
		}
		
		if(StringUtils.isNotBlank(cqo.getCd_begin())) {
			cdSql.append(" and cd_publish >= #{cd_begin}");
		}
		
		if(StringUtils.isNotBlank(cqo.getCd_end())){
			cdSql.append(" and cd_publish <= #{cd_end}");
		}
		System.out.println(cdSql);
		return cdSql.toString();
	}
	
	/**
	 * 添加一个新的专辑,专辑数据的字段不固定
	 * @param map 专辑信息
	 * @return 返回一条sql语句
	 */
	public String addCDSql(Map<String, Object> map) {
		
		StringBuilder cdSql = new StringBuilder();
		cdSql.append("INSERT INTO mc_cd (cd_name,");
		
		if(StringLength.stringLength(map.get("cd_like")) != 0) {
			cdSql.append("cd_like,");
		}
		if(StringLength.stringLength(map.get("cd_count")) != 0) {
			cdSql.append("cd_count,");
		}
		if(StringLength.stringLength(map.get("cd_publish")) != 0) {
			cdSql.append("cd_publish,");
		}
		cdSql.append("singer_id,");
		/*if(StringLength.stringLength(map.get("cd_cover")) != 0) {
			cdSql.append("cd_cover,");
		}*/
		if(StringLength.stringLength(map.get("cd_status")) != 0) {
			cdSql.append("cd_status,");
		}
		if(StringLength.stringLength(map.get("cd_info")) != 0) {
			cdSql.append("cd_info,");
		}
		
		cdSql = new StringBuilder(cdSql.substring(0, cdSql.length()-1));
		
		cdSql.append(") VALUES (#{cd_name},");
		if(StringLength.stringLength(map.get("cd_like")) != 0) {
			cdSql.append("#{cd_like},");
		}
		if(StringLength.stringLength(map.get("cd_count")) != 0) {
			cdSql.append("#{cd_count},");
		}
		if(StringLength.stringLength(map.get("cd_publish")) != 0) {
			cdSql.append("#{cd_publish},");
		}
		cdSql.append("#{singer_id},");
		/*if(StringLength.stringLength(map.get("cd_cover")) != 0) {
			cdSql.append("#{cd_cover},");
		}*/
		if(StringLength.stringLength(map.get("cd_status")) != 0) {
			cdSql.append("#{cd_status},");
		}
		if(StringLength.stringLength(map.get("cd_info")) != 0) {
			cdSql.append("#{cd_info},");
		}
		
		cdSql = new StringBuilder(cdSql.substring(0, cdSql.length()-1));
		cdSql.append(")");
		System.out.println(cdSql.toString());
		
		return cdSql.toString();
	}
	
	/**
	 * 修改一个专辑的信息,修改的数据字段不固定
	 * @param map 专辑信息
	 * @return 返回一条sql语句
	 */
	public String updateCDSql(Map<String, Object> map) {
		StringBuilder cdSql = new StringBuilder();
		
		cdSql.append("update mc_cd set cd_name=#{cd_name},");
		
		if(StringLength.stringLength(map.get("cd_like")) != 0) {
			cdSql.append("cd_like=#{cd_like},");
		}
		if(StringLength.stringLength(map.get("cd_count")) != 0) {
			cdSql.append("cd_count=#{cd_count},");
		}
		if(StringLength.stringLength(map.get("cd_publish")) != 0) {
			cdSql.append("cd_publish=#{cd_publish},");
		}
		cdSql.append("singer_id=#{singer_id}");
		/*if(StringLength.stringLength(map.get("cd_cover")) != 0) {
			cdSql.append("cd_cover,");
		}*/
		if(StringLength.stringLength(map.get("cd_status")) != 0) {
			cdSql.append(",cd_status=#{cd_status}");
		}
		if(StringLength.stringLength(map.get("cd_info")) != 0) {
			cdSql.append(",cd_info=#{cd_info}");
		}
		
		cdSql.append(" where cd_id=#{cd_id}");
		
		return cdSql.toString();
	}
}
